<template>
  <div>
    <van-icon
    :color="value?'#3296fa':'#777'"
    :name="value?'star':'star-o'"
    @click="onCollect"
    />
  </div>
</template>

<script>
import { addCollectArticle, deleteCollectArticle } from '@/api/article.js'
import { Toast } from 'vant'

export default {
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Number, Object, String]
    }
  },
  data () {
    return {}
  },

  created () {},

  methods: {
    async onCollect () {
      Toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      try {
        if (this.value) {
        // 点击取消收藏
          await deleteCollectArticle(this.artId)
        } else {
        // 点击收藏
          await addCollectArticle(this.artId)
        }
        this.$emit('input', !this.value)
        Toast.success('收藏成功')
      } catch (err) {
        Toast.fail('收藏失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
